//
//  SelectAreaPrefixView.m
//  ICrEAM
//
//  Created by FoREVer_SaD on 16/11/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SelectAreaPrefixView.h"
//#import "ParameterAction.h"

@interface SelectAreaPrefixView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *prefixCollectionView;

@end

@implementation SelectAreaPrefixView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        NSArray *array = [ICrEAMTool getParameterArrayWithCode:@"2147"];
        
        _dataArray = [[ICrEAMTool getParameterTitleArrayWithArray:array] mutableCopy];
        NSMutableArray *midArray = [NSMutableArray array];
        for (NSString *data in _dataArray) {
            if (data.length != 1) {
                [midArray addObject:data];
            }
        }
        [_dataArray addObject:@"其他"];
        [_dataArray removeObjectsInArray:midArray];
    }
    return _dataArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        
        UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        blankView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
        [blankView addGestureRecognizer:tap];
        [self addSubview:blankView];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(ActualWidth(50), ActualHeight(30));
        layout.minimumLineSpacing = ActualWidth(3);
        layout.minimumInteritemSpacing = ActualWidth(3);
        layout.sectionInset = UIEdgeInsetsMake(ActualWidth(10), ActualWidth(10), ActualWidth(10), ActualWidth(10));
        UICollectionView *prefixCollectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        prefixCollectionView.backgroundColor = BackgroundColor_White;
        prefixCollectionView.delegate = self;
        prefixCollectionView.dataSource = self;
        prefixCollectionView.bounces = NO;
        [self addSubview:prefixCollectionView];
        self.prefixCollectionView = prefixCollectionView;
        [prefixCollectionView registerClass:[SelectAreaPrefixViewCell class] forCellWithReuseIdentifier:@"SelectAreaPrefixViewCell"];
        CornerRadius(prefixCollectionView, RadiusValue);
        BorderWidth(prefixCollectionView, 1.0);
        BorderColor(prefixCollectionView, BorderColor_Gray_Hex);
    }
    return self;
}

- (void)hideView {
    [self removeFromSuperview];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SelectAreaPrefixViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectAreaPrefixViewCell" forIndexPath:indexPath];
    cell.title = self.dataArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(didSelectAreaPrefix:)]) {
        [self.delegate didSelectAreaPrefix:self.dataArray[indexPath.row]];
    }
    [self removeFromSuperview];
}

@end


@interface SelectAreaPrefixViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@end


@implementation SelectAreaPrefixViewCell

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
    _title = title;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        CornerRadius(self, RadiusValue);
        BorderWidth(self, 1.0);
        BorderColor(self, BorderColor_Gray_Hex);
        
        self.backgroundColor = [UIColor clearColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = ContentFont;
        titleLabel.tintColor = ContentTextColor_Black;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel = titleLabel;
        [self.contentView addSubview:titleLabel];
        
        __weak typeof(self) weakSelf = self;
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(weakSelf.mas_width);
            make.height.equalTo(@(ActualHeight(15)));
            make.centerX.equalTo(weakSelf.mas_centerX);
            make.centerY.equalTo(weakSelf.mas_centerY);
        }];
    }
    return self;
}

@end
